home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / lib / readline.c < prev    next >
Text File  |  1989-12-17  |  716b  |  35 lines

  1. /*
  2.  * Read a line from a descriptor.  Read the line one byte at a time,
  3.  * looking for the newline.  We store the newline in the buffer,
  4.  * then follow it with a null (the same as fgets(3)).
  5.  * We return the number of characters up to, but not including,
  6.  * the null (the same as strlen(3)).
  7.  */
  8.  
  9. int
  10. readline(fd, ptr, maxlen)
  11. register int    fd;
  12. register char    *ptr;
  13. register int    maxlen;
  14. {
  15.     int    n, rc;
  16.     char    c;
  17.  
  18.     for (n = 1; n < maxlen; n++) {
  19.         if ( (rc = read(fd, &c, 1)) == 1) {
  20.             *ptr++ = c;
  21.             if (c == '\n')
  22.                 break;
  23.         } else if (rc == 0) {
  24.             if (n == 1)
  25.                 return(0);    /* EOF, no data read */
  26.             else
  27.                 break;        /* EOF, some data was read */
  28.         } else
  29.             return(-1);    /* error */
  30.     }
  31.  
  32.     *ptr = 0;
  33.     return(n);
  34. }
  35.